package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.mapper.EduVideoMapper;
import com.atguigu.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-03-02
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {
    @Autowired
    private VodClient vodClient;

    @Override
    public void removeByCourseId(String courseId) {
        QueryWrapper<EduVideo> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("course_id",courseId);
        videoQueryWrapper.select("video_source_id");
        List<EduVideo> videoList = baseMapper.selectList(videoQueryWrapper);
        if(videoList.size() > 0){
            ArrayList<String> videoIdList = new ArrayList<>();
            for (int i = 0; i < videoList.size(); i++) {
                String videoSourceId = videoList.get(i).getVideoSourceId();
                if (!StringUtils.isEmpty(videoSourceId)){
                    videoIdList.add(videoSourceId);
                }
            }
            if (videoIdList.size()> 0){
                vodClient.deleteBatch(videoIdList);
            }
            QueryWrapper<EduVideo> objectQueryWrapper = new QueryWrapper<>();
            objectQueryWrapper.eq("course_id",courseId);
            baseMapper.delete(objectQueryWrapper);
        }
    }
}
